<template>
  <div class="xiecheng">
    <div class="admin_main_block">
      <div class="admin_main_block_top">
        <div class="admin_main_block_left">
          <div>
            <el-button
              v-if="isAuth('module:education:course:form')"
              type="success"
              icon="Plus"
              @click="router.push({ name: 'module_education_course_form' })">
              {{ i18n('common.create') }}
            </el-button>
          </div>
        </div>

        <div class="admin_main_block_right">
          <div>
            <el-button
              v-if="isAuth('module:education:course:delete')"
              type="danger"
              icon="Delete"
              @click="deleteHandle()">
              {{ i18n('common.batch_delete') }}
            </el-button>
          </div>
        </div>
      </div>

      <div class="admin_main_block_top">
        <div class="admin_main_block_select">
          <div class="input">
            <el-select
              v-model="dataForm.category_id"
              :placeholder="i18n('common.please_select') + i18n('course.category_id')"
              clearable>
              <el-option :label="i18n('common.all')" value=""></el-option>
              <el-option v-for="(v, k) in categoryList" :label="v.title" :key="k" :value="v.id">
              </el-option>
            </el-select>
          </div>
          <div class="input">
            <el-input v-model="dataForm.title" :placeholder="$t('common.please_input') + $t('course.title')" clearable>
            </el-input>
          </div>
          <div class="input">
            <el-select
              v-model="dataForm.type"
              :placeholder="i18n('common.please_select') + i18n('course.type')"
              clearable>
              <el-option :label="i18n('common.all')" value=""></el-option>
              <el-option v-for="(v, k) in typeList" :label="v.text" :key="k" :value="v.value">
              </el-option>
            </el-select>
          </div>
          <div class="input">
            <el-select v-model="dataForm.is_recommend" :placeholder="$t('common.please_select') + $t('course.is_recommend')" clearable>
              <el-option :label="$t('common.all')" value=""></el-option>
              <el-option :label="$t('course.enable')" value="1"></el-option>
              <el-option :label="$t('course.disable')" value="2"></el-option>
            </el-select>
          </div>
          <div class="input">
            <el-select v-model="dataForm.is_hidden" :placeholder="$t('common.please_select') + $t('common.is_hidden')" clearable>
              <el-option :label="$t('common.all')" value=""></el-option>
              <el-option :label="$t('common.hidden')" value="1"></el-option>
              <el-option :label="$t('common.show')" value="2"></el-option>
            </el-select>
          </div>
          <div class="input">
            <el-date-picker v-model="dataForm.create_time" type="daterange" :range-separator="$t('common.to')" :start-placeholder="$t('common.start_time')" :end-placeholder="$t('common.end_time')" clearable>
            </el-date-picker>
          </div>
          <div class="input">
            <el-button icon="Search" @click="getDataList(true)">
              {{ i18n('common.search') }}
            </el-button>
          </div>
        </div>
      </div>
      <div class="admin_table_main">
        <el-table :data="dataList" v-loading="dataListLoading" @selection-change="selectionChangeHandle" stripe>
          <el-table-column type="selection" header-align="center" align="center">
          </el-table-column>

          <el-table-column prop="id" :label="i18n('common.id')" width="70">
          </el-table-column>

          <el-table-column :label="$t('course.category_id')" width="80">
            <template v-slot="scope">
              <span v-if="scope.row.category">
                {{ scope.row.category.title }}
              </span>
            </template>
          </el-table-column>

          <el-table-column :label="i18n('course.info')">
            <template v-slot="scope">
              <dl class="table_dl">
                <dt>
                  <el-image class="cx-list-image" :src="scope.row.picture" loading="lazy" lazy>
                    <template #error>
                      <div class="image-slot">
                        <el-icon><Picture /></el-icon>
                      </div>
                    </template>
                  </el-image>
                </dt>
                <dd class="table_dl_dd_all_30">
                  {{ i18n('course.title') }}： {{ scope.row.title }}
                </dd>
                <dd class="table_dl_dd_all_16_gray">
                  {{ i18n('course.create_time') }}： {{ scope.row.create_time }}
                </dd>
              </dl>
            </template>
          </el-table-column>

          <el-table-column :label="$t('course.type')" width="80">
            <template v-slot="scope">
              {{ scope.row.type.text }}
            </template>
          </el-table-column>

          <el-table-column prop="money" :label="$t('course.money')" width="80">
          </el-table-column>

          <el-table-column prop="time_length" :label="$t('course.time_length')" width="120">
            <template v-slot="scope">
              <span v-if="scope.row.statistical">
                {{ scope.row.statistical.course_time_length }}
              </span>
            </template>
          </el-table-column>

          <el-table-column prop="sort" :label="i18n('common.sort')" width="80">
          </el-table-column>

          <el-table-column :label="$t('course.is_recommend')" width="80">
            <template v-slot="scope">
              <el-switch
                v-model="scope.row.is_recommend.value"
                active-value="1"
                inactive-value="2"
                size="large"
                inline-prompt
                :active-text="$t('common.yes')"
                :inactive-text="$t('common.no')"
                style="--el-switch-on-color: #13ce66; --el-switch-off-color: #ff4949"
                @change="handleStatus($event, scope.row.id, 'is_recommend')">
              </el-switch>
            </template>
          </el-table-column>

          <el-table-column :label="$t('common.is_hidden')" width="80">
            <template v-slot="scope">
              <el-switch
                v-model="scope.row.is_hidden.value"
                active-value="1"
                inactive-value="2"
                size="large"
                inline-prompt
                :active-text="$t('common.yes')"
                :inactive-text="$t('common.no')"
                style="--el-switch-on-color: #ff4949; --el-switch-off-color: #13ce66"
                @change="handleStatus($event, scope.row.id, 'is_hidden')">
              </el-switch>
            </template>
          </el-table-column>

          <el-table-column :label="i18n('common.handle')" fixed="right" width="570">
            <template v-slot="scope">
              <el-button
                v-if="isAuth('module:education:course:view')"
                type="info"
                icon="View"
                @click="router.push({ name: 'module_education_course_view', query: { id: scope.row.id } })">
                {{ i18n('common.view') }}
              </el-button>

              <el-button
                v-if="isAuth('module:education:course:form')"
                type="primary"
                icon="Edit"
                @click="router.push({ name: 'module_education_course_form', query: { id: scope.row.id } })">
                {{ i18n('common.update') }}
              </el-button>

              <el-button
                v-if="isAuth('module:education:course:unit:list')"
                icon="CopyDocument"
                @click="$router.push({name: 'module_education_course_unit_list', query: {course_id: scope.row.id}})">
                {{ $t('course.course_unit') }}
              </el-button>

              <el-button
                v-if="isAuth('module:education:course:intensify:list')"
                class="el-button--cyan"
                icon="Tickets"
                @click="$router.push({name: 'module_education_course_intensify_list', query: {course_id: scope.row.id}})">
                {{ $t('course.intensify_info') }}
              </el-button>

              <el-button
                v-if="isAuth('module:education:course:structure:data')"
                class="el-button--purple"
                icon="Share"
                @click="$router.push({name: 'module_education_course_structure_data', query: {course_id: scope.row.id}})">
                {{ $t('course.course_structure') }}
              </el-button>

              <el-button
                v-if="isAuth('module:education:course:delete')"
                type="danger"
                icon="Delete"
                @click="deleteHandle(scope.row.id)">
                {{ i18n('common.delete') }}
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="admin_table_main_pagination">
          <el-pagination
            @size-change="sizeChangeHandle"
            @current-change="currentChangeHandle"
            :page-size="pageSize"
            :total="totalPage"
            :current-page="pageIndex"
            background
            layout="prev, pager, next,jumper,total">
          </el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup name="course-list">
  import { useBase } from "@/hooks/base/useBase"
  import { useTable } from "@/hooks/table/useTable"

  const { router, query, i18n, isAuth } = useBase()

  const model = ref('education/course')

  const categoryList = ref([])
  const typeList = ref([])

  const dataForm = reactive([
    'category_id',
    'type',
    'title',
    'is_recommend',
    'is_hidden',
    'create_time',
  ])

  const {
    dataList,
    pageIndex,
    pageSize,
    totalPage,
    dataListLoading,
    sizeChangeHandle,
    currentChangeHandle,
    selectionChangeHandle,
    getDataList,
    handleStatus,
    deleteHandle,
  } = useTable(model.value, dataForm)


  const loadCategoryList = () => {
    $http({
      url: $http.adornUrl('/education/category/select'),
      method: 'get',
    }).then(({ data }) => {
      if (data && data.status === 200) {
        categoryList.value = data.data
      } else {
        $message.error(i18n(data.message))
      }
    })
  }

  const loadTypeList = () => {
    $http({
      url: $http.adornUrl('/dictionary/data'),
      method: 'get',
      params: $http.adornParams({
        title: 'education_course_type',
      }),
    }).then(({data}) => {
      if (data && data.status === 200) {
        typeList.value = data.data
      } else {
        $message.error(i18n(data.message))
      }
    })
  }

  onMounted(() => {
    getDataList(model)

    loadCategoryList()
    loadTypeList()
  })
</script>
